import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: AnimateSwitchPage());
  }
}

class AnimateSwitchPage extends StatefulWidget {
  @override
  _AnimateSwitchPageState createState() => _AnimateSwitchPageState();
}

class _AnimateSwitchPageState extends State<AnimateSwitchPage> {
  int count = 1;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('动画示例')),
      body: SizedBox.expand(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            AnimatedSwitcher(
              duration: Duration(milliseconds: 400),
              // transitionBuilder: (Widget child, Animation<double> animation) {
              //   return ScaleTransition(scale: animation, child: child);
              // },
              transitionBuilder: (Widget child, Animation<double> animation) {
                // 判断动画方向
                final isEntering = animation.status != AnimationStatus.reverse;

                final slideAnimation = Tween<Offset>(
                  begin: isEntering ? Offset(0.0, -1.0) : Offset.zero,
                  end: isEntering ? Offset.zero : Offset(0.0, 1.0),
                ).animate(animation);

                return SlideTransition(position: slideAnimation, child: child);
              },
              child: Text(
                "$count",
                key: ValueKey('$count'),
                style: TextStyle(fontSize: 66, fontWeight: FontWeight.bold),
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            count++;
          });
        },
        child: Icon(Icons.add),
      ),
    );
  }
}
